unit common;

{$mode objfpc}
{$H+}

interface


uses
  gtk2, glib2, gdk2pixbuf, classes;
  
type
  
  PIcon = ^TIcon;
  TIcon = record
    FIcon : PGdkPixbuf;
    FKey  : String;
  end;//TIcon
  //-------------------------------
  PIconCenter = ^TIconCenter;
  TIconCenter = class(TObject)
    private
      FIconList:TList;
    public
      constructor Create;
      destructor  Destroy; override;
      function    GetIcon(key:String):PGdkPixbuf;
  end;//TIconCenter
  //-------------------------------
  TProcessInfo = class
    private
      FPID    : integer;
      FUser   : string;
      FServer : string;
      FDescript : string;
      FRPort  : integer; //remote port
      FLPort  : integer; //local port
    public
      constructor Create(Line : string);
      constructor Create(TheUser : string; TheServer : string; TheDescript : string; TheRPort, TheLPort : integer);
      destructor  Destroy; override;
      property PID : integer read FPID write FPID;
      property User : string read FUser write FUser;
      property Server : string read FServer write FServer;
      property Descript : string read FDescript write FDescript;
      property RPort : integer read FRPort write FRPort;
      property LPort : integer read FLPort write FLPort;
  end;//TProcessInfo
  
  
var
  Icons : TIconCenter;
  
  
implementation
  
  
uses
  sysutils;
  
  
  constructor TIconCenter.Create;
  {$include images/window.pas_xpm}
  {$include images/connected.pas_xpm}
  {$include images/disconnected.pas_xpm}
  {$include images/add.pas_xpm}
  {$include images/delete.pas_xpm}
  {$include images/exit.pas_xpm}
  {$include images/refresh.pas_xpm}
  var
    AIcon:PIcon;
  begin
    FIconList:=TList.Create;
    
    new(AIcon);
    AIcon^.FKey:='window';
    AIcon^.FIcon:=gdk_pixbuf_new_from_xpm_data(@window_xpm[1]);
    FIconList.Add(AIcon);
    
    new(AIcon);
    AIcon^.FKey:='connected';
    AIcon^.FIcon:=gdk_pixbuf_new_from_xpm_data(@connected_xpm[1]);
    FIconList.Add(AIcon);
    
    new(AIcon);
    AIcon^.FKey:='disconnected';
    AIcon^.FIcon:=gdk_pixbuf_new_from_xpm_data(@disconnected_xpm[1]);
    FIconList.Add(AIcon);

    new(AIcon);
    AIcon^.FKey:='add';
    AIcon^.FIcon:=gdk_pixbuf_new_from_xpm_data(@add_xpm[1]);
    FIconList.Add(AIcon);

    new(AIcon);
    AIcon^.FKey:='delete';
    AIcon^.FIcon:=gdk_pixbuf_new_from_xpm_data(@delete_xpm[1]);
    FIconList.Add(AIcon);

    new(AIcon);
    AIcon^.FKey:='exit';
    AIcon^.FIcon:=gdk_pixbuf_new_from_xpm_data(@exit_xpm[1]);
    FIconList.Add(AIcon);

    new(AIcon);
    AIcon^.FKey:='refresh';
    AIcon^.FIcon:=gdk_pixbuf_new_from_xpm_data(@refresh_xpm[1]);
    FIconList.Add(AIcon);
  end;//TIconCenter.Create
  {--------------------------}
  destructor TIconCenter.Destroy;
  var
    AIcon : PIcon;
    i     : integer;
  begin
    for i:=0 to FIconList.Count-1 do begin
      AIcon:=PIcon(FIconList.Items[i]);
      dispose(AIcon); 
    end;//for
    FreeAndNil(FIconList);
    inherited Destroy;
  end;//TIconCenter.Destroy
  {--------------------------}
  function TIconCenter.GetIcon(key:String):PGdkPixbuf;
  var
    AIcon : PIcon;
    i     : integer;
  begin
    for i:=0 to FIconList.Count-1 do begin
      AIcon:=PIcon(FIconList.Items[i]);
      if AIcon^.FKey=key then begin
        Result:=AIcon^.FIcon;
        exit;
      end;
    end;//for
    Result:=nil;
  end;//TIconCenter.GetIcon
  
  
  constructor TProcessInfo.Create(Line : string);
  var
    i, n : integer;
    cpos : array [1..5] of integer;
  begin
    inherited Create;
    FPID := 0;
    
    n := 0;
    for i:=1 to length(Line) do begin
      if Line[i]=',' then begin
        Inc(n);
        cpos[n] := i;
        if n=4 then break;
      end;
    end;//for i
    if n<4 then raise Exception.Create('invalid profile line!');
    cpos[5] := length(Line)+1;
    
    FServer := Copy(Line, 1, cpos[1]-1);
    FUser   := Copy(Line, cpos[1]+1, cpos[2]-cpos[1]-1);
    FRPort  := StrToInt(Copy(Line, cpos[2]+1, cpos[3]-cpos[2]-1));
    FLPort  := StrToInt(Copy(Line, cpos[3]+1, cpos[4]-cpos[3]-1));
    FDescript := Copy(Line, cpos[4]+1, cpos[5]-cpos[4]-1);
  end;//TProcessInfo.Create
  //-------------------------
  constructor TProcessInfo.Create(TheUser : string; TheServer : string; TheDescript : string; TheRPort, TheLPort : integer);
  begin
    inherited Create;
    FUser   := TheUser;
    FServer := TheServer;
    FDescript := TheDescript;
    FRPort  := TheRPort;
    FLPort  := TheLPort;
  end;//TProcessInfo.Create
  //-------------------------
  destructor TProcessInfo.Destroy;
  begin
    inherited Destroy;
  end;//TProcessInfo.Destroy
  
  
initialization
  gtk_init(@argc, @argv);
  Icons := TIconCenter.Create;
  
  
finalization
  FreeAndNil(Icons);
  
  
end.//unit common

